package com.xiguaji.tiktok.v1.ins.dic.service;

import com.xiguaji.tiktok.v1.ins.dic.model.*;
import com.xiguaji.tiktok.v1.ins.dic.repository.*;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class DicService {

    @Autowired
    InsDicGenerationRepository insDicGenerationRepository;

    @Autowired
    InsDicLanguageRepository insDicLanguageRepository;

    @Autowired
    InsDicNationRepository insDicNationRepository;

    @Autowired
    InsDicCategoryRepository insDicCategoryRepository;

    @Autowired
    InsDicCommercialCategoryRepository insDicCommercialCategoryRepository;

    public List<InsDicGeneration> getDicGenerationByType(int type){
        return insDicGenerationRepository.findByGenerationTypeOrderById(type);
    }

    public List<InsDicNation> getNations(Integer type){
        if(null != type){
            return insDicNationRepository.findByTypeOrderByName(type);
        }
        return insDicNationRepository.findAllByOrderByName();
    }

    public List<InsDicLanguage> getLanguage(Integer type){
        if(null != type){
            return insDicLanguageRepository.findByTypeOrderByName(type);
        }
        return insDicLanguageRepository.findAllByOrderByName();
    }

    public List<InsDicCategory> getOverallCategory(){
        return insDicCategoryRepository.findOverall();
    }

    public List<InsDicCategory> getCategoryByOverallCategory(String overall){
        return insDicCategoryRepository.findByOverallCategoryOrderByCategory(overall);
    }

    public String getCode(String nationName){
        return insDicNationRepository.findCodeByName(nationName);
    }

    /**
     * 一级商业分类
     * @return
     */
    public List<InsDicCommercialCategory> findCommercialCategory(){
        return insDicCommercialCategoryRepository.findCommercial();
    }

    /**
     * 获取语言字典
     * @return
     */
    public Map<String,String> getDicLanguageMap(){
        val dicLanguages = getLanguage(null);
        if(dicLanguages != null && !dicLanguages.isEmpty()){
            Iterator<InsDicLanguage> dicLanguageList = dicLanguages.iterator();
            Map<String,String> dicMap = new HashMap<>(dicLanguages.size());
            InsDicLanguage dicLanguage;
            while (dicLanguageList.hasNext()){
                dicLanguage = dicLanguageList.next();
                if(dicLanguage != null && StringUtils.isNotEmpty(dicLanguage.getCode())){
                    dicMap.put(dicLanguage.getCode().toLowerCase(),dicLanguage.getName());
                }
            }
            return dicMap;
        }
        return new HashMap<>(1);
    }

    /**
     * 获取国家字典
     * @return
     */
    public Map<String,String> getDicNationMap(){
        val dicNations = getNations(null);
        if(dicNations != null && !dicNations.isEmpty()){
            Iterator<InsDicNation> dicNationList = dicNations.iterator();
            Map<String,String> dicMap = new HashMap<>(dicNations.size());
            InsDicNation dicNation;
            while (dicNationList.hasNext()){
                dicNation = dicNationList.next();
                if(dicNation != null && StringUtils.isNotEmpty(dicNation.getCode())){
                    dicMap.put(dicNation.getCode(),dicNation.getName());
                }
            }
            return dicMap;
        }
        return new HashMap<>(1);
    }
}
